﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace LicenseManager
{
    public  class SE
    {
        private const string EncryptKey = "A_db9Fu8*OSasBe3";
        private static readonly byte[] IV =
            new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

        /// <summary>
        /// TripleDES Encrypt
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Encrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Encoding.ASCII.GetBytes(s);

                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = ASCIIEncoding.ASCII.GetBytes(EncryptKey);
                des.IV = IV;
                result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }

        /// <summary>
        ///TripleDES Decrypts provided string parameter
        /// </summary>
        public static string Decrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Convert.FromBase64String(s);

                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

                des.Key = ASCIIEncoding.ASCII.GetBytes(EncryptKey);

                des.IV = IV;

                result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }

        /// <summary>
        /// MD5 Encryption with no Decrypt
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Encryption(string password)
        {
            byte[] value;
            string pwd = string.Empty;
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            UTF8Encoding encoding = new UTF8Encoding();
            value = md5Hasher.ComputeHash(encoding.GetBytes(password));
            for (int i = 0; i < value.Length; i++)
            {
                pwd = pwd + value[i].ToString("X").PadLeft(2, '0');
            }
            return pwd;
        }

        private const string RSAPublicKey = "";

       
    }
}
